{extend name="extend:iframe" /}

{block name="css"}
<link rel="stylesheet" type="text/css" href="/public/admin/css/layout.global.css" />
<link rel="stylesheet" type="text/css" href="/public/plug/FormCombox/FormCombox.css" />
<style type="text/css">
	.layout-form {
		padding-left: 0;
		padding-right: 16px;
	}
	.layout-form * {
		font-size: 14px;
	}
</style>
{/block}


{block name="body"}
<div class="_title">
    <div class="_main">友情链接</div>
    <div class="_sub clearfix">
        <div class="_btns">
            <ul class="clearfix">
                <li layout-mark="link" data-href="/admin/link.html">链接列表</li>
                <li class="active">新增链接</li>
            </ul>
        </div>
    </div>
</div>
<div class="_content">
	<div class="layout-form p-t-6x" id="layout-form-link">
		<div class="layout-combox "><label class="layout-form-label">名称</label>
			<div class="layout-form-ipt"><input type="text" name="title"></div>
		</div>
		<div class="layout-combox "><label class="layout-form-label">链接</label>
			<div class="layout-form-ipt"><input type="text" name="url" value="javascript:;"></div>
		</div>
		<div class="layout-combox "><label class="layout-form-label">排序</label>
			<div class="layout-form-ipt"><input type="text" name="sort" value="0"></div>
		</div>
	</div>
	<div class="box-buttom clearfix p-b-4x" style="width: 240px; padding-left: 100px;">
		<a href="javascript:;" class="submit-buttom float-left" id="btn-link-submit">确定</a>
	</div>
</div>
{/block}


{block name="js"}
<script type="text/javascript" src="/public/plug/layer/layer.min.js"></script>
<script type="text/javascript" src="/public/modules/tool.js"></script>
<script type="text/javascript" src="/public/modules/interactive.js"></script>
<script type="text/javascript">
	var linkid = tool.getSearch('linkid');
	if (linkid && linkid > 0) {
		layer.load(0, {
			shade: [0.08, '#000']
		});
		ajax().get('/api?link.admin.get', {
			'id': linkid
		}, function(res) {
			layer.closeAll('loading');
			if(res.status == 200) {
				var list = res.data;
				$('input[name=title]').val(list.title);
				$('input[name=url]').val(list.url);
				$('input[name=sort]').val(list.sort);
				initLink();
			} else {
				window.parent.system.error(res.error);
			}
		});
	} else {
		initLink();
	}
	
	function initLink() {
		// 重置页面高度
		resetIframeHeight();
	}
	
	$(function() {
		// 页面跳转
		$('li[layout-mark=link]').on('click', function() {
			window.location.href = $(this).attr('data-href');
		});
		
		// 提交
		$('#btn-link-submit').on('click', submitLink);
	});
	
	// 提交
	function submitLink() {
		var data_opt = {};
		$('#layout-form-link').find('input[type=text]').each(function() {
			var name = this.name;
			data_opt[name] = this.value;
		});
		
		if (linkid && linkid > 0) {
			data_opt['id'] = linkid;
			submitLinkEdit(data_opt);
		} else {
			submitLinkAdd(data_opt);
		}
	}
	
	// 添加
	function submitLinkAdd(data_opt) {
		layer.load(0, {
			shade: [0.08, '#000']
		});
		ajax().post('/api?link.admin.add', data_opt, function(res) {
			layer.closeAll('loading');
			if(res.status == 201) {
				window.parent.system.success('添加成功', function() {
					window.location.href = '/admin/link.html';
				});
			} else {
				window.parent.system.error(res.error);
			}
		});
	}
	
	// 编辑
	function submitLinkEdit(data_opt) {
		layer.load(0, {
			shade: [0.08, '#000']
		});
		ajax().put('/api?link.admin.edit', data_opt, function(res) {
			layer.closeAll('loading');
			if(res.status == 201) {
				window.parent.system.success('编辑成功', function() {
					window.location.href = '/admin/link.html';
				});
			} else {
				window.parent.system.error(res.error);
			}
		});
	}
</script>

{/block}